package dwz.business.menu.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.wx.util.CodeUtils;
import dwz.business.menu.Menu;
import dwz.business.menu.MenuLevel;
import dwz.business.menu.MenuServiceMgr;
import dwz.business.menu.MenuStatus;
import dwz.framework.sys.business.AbstractBusinessObjectServiceMgr;
import dwz.framework.sys.exception.ServiceException;
import dwz.persistence.BaseConditionVO;
import dwz.persistence.beans.WechatMenu;
import dwz.persistence.mapper.WechatMenuMapper;


@Transactional(rollbackFor = Exception.class)
@Service(MenuServiceMgr.SERVICE_NAME)
public class MenuServiceMgrImpl extends AbstractBusinessObjectServiceMgr implements MenuServiceMgr {
	
	@Autowired
	private WechatMenuMapper menuMapper;
	
	public List<Menu> searchMenu(BaseConditionVO vo){
		List<Menu> bos = new ArrayList<Menu>();
		RowBounds rb = new RowBounds(vo.getStartIndex(), vo.getPageSize());
		List<WechatMenu> pos = menuMapper.findPageBreakByCondition(vo, rb);
		for (WechatMenu po : pos) {
			bos.add(new Menu(po));
		}
		return bos;
	}

	public Integer searchUserNum(BaseConditionVO vo) {
		Integer count = menuMapper.findNumberByCondition(vo);
		return count;
	}
	
	/**
	 * 根据父级查询菜单列表信息
	 */
	public List<Menu> findMenuByParentCode(String parentCode){
		List<Menu> bos = new ArrayList<Menu>();
		List<WechatMenu> pos = menuMapper.findMenuByParentCode(parentCode);
		for (WechatMenu po : pos) {
			bos.add(new Menu(po));
		}
		return bos;
	}
	
	public Menu getMenu(int id){
		WechatMenu po = menuMapper.load(id);
		if (po == null) return null;
		return new Menu(po);
	}
	
	public WechatMenu getMenuByKeyName(String keyName){
		WechatMenu po=menuMapper.getMenuByKeyName(keyName);
		return po;
	}
	
	public void updateMenu(Menu menu){
		Date now = new Date();
		WechatMenu po = menu.getWechatMenu();
		po.setUpdateTime(now);
		
		menuMapper.updateSelective(po);
	}
	public void updateMenuStatus(int id,String status){
		menuMapper.updateStatus(id, status, new Date());
	}
	
	public void addMenu(Menu menu)throws ServiceException{
		Date now = new Date();
		WechatMenu po = menu.getWechatMenu();
		if(po!=null){
			po.setCreateTime(now);
			po.setUpdateTime(now);
			po.setStatus(MenuStatus.ACTIVE.toString());
			po.setLevel(MenuLevel.FIRST.toString());
			po.setCode(CodeUtils.getCode());
			po.setParentCode("0");
			menuMapper.insert(po);
		}
	}
	
	public void addSecondMenu(Menu menu)throws ServiceException{
		Date now = new Date();
		WechatMenu po = menu.getWechatMenu();
		if(po!=null){
			po.setCreateTime(now);
			po.setUpdateTime(now);
			po.setStatus(MenuStatus.ACTIVE.toString());
			po.setLevel(MenuLevel.SECOND.toString());
			po.setCode(CodeUtils.getCode());
			menuMapper.insert(po);
		}
	}
	
	
	public Boolean isExistMenuKey(String keyName)throws ServiceException{
		WechatMenu wm= this.getMenuByKeyName(keyName);
		if(wm!=null){
			return true;
		}
		return false;
	}
	
	
}
